# coding:utf-8
import urllib

import os
import json
import sys
import datetime
import set_env_path  # 请勿删除

from sqlalchemy import and_

from common.constant import RECEIVE_RED_PACKET_HISTORY
from common.models import model_factory
from common.utils import DB, redis_client


def init_red_packet_cache():
    """
    create by ld_wu 2018-06-11
    更新跟进消息类型
    """

    def get_history(receiver_record, current_list):
        receiver = model_factory.user.get_by_id(session, receiver_record.receiver_id)
        if receiver:
            if receiver.avatar:
                receiver__avatar = urllib.parse.urljoin(host, receiver.avatar)
            else:
                receiver__avatar = receiver.avatar
            nickname = receiver.nickname
        else:
            nickname = ''
            receiver__avatar = ''
        current_list.append(dict(
            money=receiver_record.money,
            received_at=receiver_record.received_at,
            best_receiver=bool(receiver_record.best_receiver),
            receiver_id=receiver_record.receiver_id,
            receiver_nickname=nickname,
            receiver_avatar=receiver__avatar
        ))
        return current_list

    red_packet_model = model_factory.bid_point_red_packet
    get_bid_point_red_packet = model_factory.get_bid_point_red_packet

    with DB() as session:
        red_packets = session.query(
            red_packet_model
        ).all()
        for red_packet in red_packets:
            sender = model_factory.user.get_by_id(session, red_packet.sender_id)
            if sender:
                sender_nickname = sender.nickname
                host = os.environ.get('img_host')
                if sender.avatar:
                    avatar = urllib.parse.urljoin(host, sender.avatar)
                else:
                    avatar = sender.avatar
                sender_avatar = avatar
            else:
                sender_nickname = ''
                sender_avatar = ''
            red_packet_info = dict(
                overed_at=red_packet.overed_at,
                total_money=red_packet.total_money,
                remark=red_packet.remark,
                packet_type=red_packet.packet_type,
                packet_count=red_packet.packet_count,
                status=red_packet.status,
                sender_id=red_packet.sender_id,
                created_at=red_packet.created_at,
                sender_nickname=sender_nickname,
                sender_avatar=sender_avatar
            )
            records = session.query(
                get_bid_point_red_packet
            ).filter(
                get_bid_point_red_packet.red_packet_id == red_packet.id,
                get_bid_point_red_packet.receiver_id != 0
            ).order_by(
                get_bid_point_red_packet.id
            ).all()
            result = list()
            for record in records:
                get_history(record, result)
            key = RECEIVE_RED_PACKET_HISTORY.format(red_packet_id=red_packet.id)
            print(red_packet.id, red_packet.total_money, red_packet.packet_count)
            print(len(result))
            redis_client.set(key, json.dumps(dict(
                result=result,
                red_packet_info=red_packet_info,
                polling=True
            )))


if __name__ == '__main__':
    init_red_packet_cache()
